package com.wawayaya;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;
import com.wbtech.ums.UmsAgent;
import java.io.File;
import java.io.FilenameFilter;

import wawayaya.kids_iread.R;

import com.wawayaya.util.Constants;
import com.wawayaya.util.FileUtil;
import com.wawayaya.util.TDevice;
import com.wawayaya.util.UIHelper;

/**
 * 启动界面
 * @author  (last update author) David
 * @version 1.0
 * @created 2015-11-9
 */
public class AppStart extends Activity {

    private Context     context;
    private String      versionname;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setContentView(R.layout.app_start);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.context = this;
      /*  UmsAgent.postClientData(context);
        UmsAgent.uploadLog(context);*/
        if (!toDelete()) {
            toMain();
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    private void  toMain(){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                UIHelper.showMainActivity(context);
            }
        }, 500);
        AppStart.this.finish();
    }

    private boolean  toDelete(){
        boolean ret = false;
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            versionname = info.versionName;
        } catch (Exception e) {
            e.printStackTrace();
        }

        String versioninfofolder= "";
        try {
            versioninfofolder = FileUtil.getireadckcsdcardpath(true);
            versionname = versionname.replace(".","_");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(versioninfofolder) && !TextUtils.isEmpty(versionname)) {
            File versionfile = null;
            versionfile = new File(versioninfofolder + File.separator + Constants.VERION_FILE_PREFIX + versionname);
            if (versionfile != null && versionfile.exists() && versionfile.isFile()) {

            } else {
                if (versionname.equalsIgnoreCase(Constants.VERION_FOR_1_0)) {
                    new DeleteOldDataTask().execute(versionfile.getAbsolutePath());
                    ret = true;
                } else {
                    File folderfile = new File(versioninfofolder);
                    File oldversionfile = null;
                    File[] oldversionfiles = folderfile.listFiles(new VersionNameFilter());
                    if (oldversionfiles != null && oldversionfiles.length > 0) {
                        oldversionfile = oldversionfiles[0];
                    }

                    if (oldversionfile != null && oldversionfile.exists() && oldversionfile.isFile()) {
                        String oldversionname = oldversionfile.getName();
                        oldversionname = oldversionname.substring(Constants.VERION_FILE_PREFIX.length());
                        if (TDevice.compareVersion(oldversionname, Constants.VERION_FOR_1_0) >= 0) {

                            oldversionfile.delete();
                            try {
                                versionfile.createNewFile();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            new DeleteOldDataTask().execute(versionfile.getAbsolutePath());
                            ret = true;
                        }
                    } else {
                        new DeleteOldDataTask().execute(versionfile.getAbsolutePath());
                        ret = true;
                    }

                }
            }
        }
        return ret;
    }

    private class DeleteOldDataTask extends AsyncTask<String, Void, Void> {
        @Override
        protected Void doInBackground(String... params) {
            /**删除原 本地文件 数据*/
            FileUtil.deleteRelativeFolers();
            File file = new File(params[0]);
            if (file != null) {
                try {
                    String folder = FileUtil.getireadckcsdcardpath(true);
                    if (!TextUtils.isEmpty(folder)) {
                        file.createNewFile();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void avoid) {
            super.onPostExecute(avoid);
            toMain();
        }
    }

    class VersionNameFilter implements FilenameFilter {
        @Override
        public boolean accept(File dir, String filename) {
            if (filename.startsWith(Constants.VERION_FILE_PREFIX)) {
                return true;
            } else {
                return false;
            }
        }
    }
}
